package com.wzh.bargain.mq;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.wzh.bargain.entity.GoodsEntity;
import com.wzh.bargain.entity.OrderBargainEntity;
import com.wzh.bargain.feign.GoodsFeign;
import com.wzh.bargain.vo.OrderVo;
import com.wzh.bargain.dao.OrderBargainDao;
import com.wzh.bargain.vo.OrderVo;
import com.wzh.common.utils.R;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author WZH
 * @title: Consumer
 * @projectName miaosha
 * @description:
 * @date 2022/2/28
 */
@Component
public class Consumer {
    @Autowired
    private OrderBargainDao orderBargainDao;

    @Autowired
    private GoodsFeign goodsFeign;

    @RabbitListener(queues ="msg3")
    public void take03(OrderVo orderVo, Channel channel, Message message) throws IOException {
        R goodsInfo = goodsFeign.info(orderVo.getGoodsId());
        if (goodsInfo.getCode()==0){
            String goodsJsonString = JSON.toJSONString(goodsInfo.get("goods"));
            GoodsEntity goods = JSON.parseObject(goodsJsonString, GoodsEntity.class);
            OrderBargainEntity order = new OrderBargainEntity();
            order.setUserId(orderVo.getUserId());
            order.setGoodsId(orderVo.getGoodsId());
            //order.setDeliveryAddrId();
            order.setGoodsName(goods.getGoodsName());
            order.setGoodsCount(1);
            order.setGoodsPrice(goods.getGoodsPrice());
            order.setGoodsChannel(1);
            order.setStatus(0);
            order.setCreateDate(new Date());
            order.setActivityId(orderVo.getActivityId());
            String orderNo = UUID.randomUUID().toString().replace("-", "");
            order.setOrderNo(orderNo);
            orderBargainDao.insert(order);
        }else {
            System.out.println("远程调用有问题");
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
